/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2012-2019.
 * Description: ekbox main file, implement main function of ekbox
 * Author: laiyuanyuan
 * Create: 2012-6-18
 */

#include <linux/module.h>
#include <linux/console.h>
#include <linux/early_kbox_internal.h>
#include <linux/early_kbox.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/memblock.h>
#include <linux/notifier.h>
#include <linux/nmi.h>
#include <linux/kmod.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h>
#include <asm/io.h>
#if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
#include <asm/memory.h>
#endif
#include <linux/slab.h>

#include <linux/syslog.h>
#include <linux/rtos_sec.h>
#include <linux/ioport.h>
#include <linux/rtos_time.h>

#if defined(CONFIG_RTOS_KASAN) && defined(CONFIG_PPC)
#include <linux/kasan.h>
#endif
#if defined(CONFIG_RTOS_KASAN) && defined(CONFIG_ARM64)
#include <internal/rtos_kasan.h>
#endif

static void ekbox_stop_record_printk(void);
static void ekbox_format_memory(void);
static int ekbox_buffer_register(void);
static int ekbox_mesg_validate(unsigned long msgno);
static void ekbox_param_msgnr_parse(const char *cmdline_ekbox);
static void ekbox_console_write(struct console *con,
				const char *buf, unsigned int len);
static ssize_t ekbox_proc_write(struct file *file, const char __user *buffer,
				size_t size, loff_t *off);

static ATOMIC_NOTIFIER_HEAD(ekbox_reserved_memory_list);

void ekbox_reboot_record(const char *func, int line)
{
#ifdef CONFIG_RTOS_PRINT_UTC
	char buf[RTOS_TIME_BUF_LEN];
#endif
	ekbox_record_point_fmt("------------------starting reboot !------------------\n");
#ifdef CONFIG_RTOS_PRINT_UTC
	ekbox_record_point_fmt("%s", get_time_str(buf, sizeof(buf)));
#endif
	ekbox_record_point_fmt("[%s:%d]\n", func, line);
}

int register_ekbox_reserved_mem_format_notifier(struct notifier_block *nb)
{
	if (nb == NULL) {
		printk(KERN_WARNING "ekbox:register_ekbox_reserved_mem_format_notifier invalid argument\n");
		return -EINVAL;
	}

	return atomic_notifier_chain_register(&ekbox_reserved_memory_list, nb);
}
EXPORT_SYMBOL(register_ekbox_reserved_mem_format_notifier);

int unregister_ekbox_reserved_mem_format_notifier(struct notifier_block *nb)
{
	if (nb == NULL) {
		printk(KERN_WARNING "ekbox:unregister_ekbox_reserved_mem_format_notifier invalid argument\n");
		return -EINVAL;
	}

	return atomic_notifier_chain_unregister(&ekbox_reserved_memory_list, nb);
}
EXPORT_SYMBOL(unregister_ekbox_reserved_mem_format_notifier);

/* five regions to store message */
#define MAX_MSG_NR	5

/* length of magic flag */
#define EKBOX_MAGIC_LEN	8

/* one message region has two magic flags */
#define NUM_MAGIC_FLAGS 2

/* size of seq_printf in single time, must less then 4096 */
#define EKBOX_ONCE_SHOW_LEN 4000

/* the offset about first message region */
#define HEAD_INFO_OFFSET    (EKBOX_MAGIC_LEN + sizeof(ekbox_head))

/* region magic flag to show if ekbox region need format */
#define EKBOX_RESERVED_MEMORY_MAGIC	0xA5A5A6A6

/* mesg magic flag to show if ekbox content validate */
#define EKBOX_MESG_MAGIC		0xA7A7A8A8

#define ekbox_printk(fmt, ...)	printk(KERN_WARNING "EKbox: "fmt, ##__VA_ARGS__)

/* every region length */
#define EKBOX_PER_MSG_LEN       ((ekbox_size - HEAD_INFO_OFFSET - EKBOX_MAGIC_LEN) / ekbox_max_msgnr)

#define EKBOX_START_MAGIC       ((unsigned long *)(ekbox_start_vir))
#define EKBOX_END_MAGIC         ((unsigned long *)(ekbox_start_vir + ekbox_size - EKBOX_MAGIC_LEN))

/* current region meassge length */
#define CURRENT_MSG_LEN         (ekbox_head_info->mesg_len[ekbox_head_info->msgno])

#define EKBOX_BUFFER_SIZE	128

#define EKBOX_AFFIX		"EKbox: "

typedef struct ekbox_head_struct {
	/* ekbox physical addr */
	unsigned long phyaddr;

	/* ekbox physical size */
	unsigned long physize;

	/* write mesg to first or second region */
	unsigned char msgno;
	unsigned char max_msgnr;
	unsigned char version[2];

	/* check change of value 'phyaddr + physize + msgno' */
	unsigned long checksum;

	/* current mesg length about five regions */
	unsigned long mesg_len[MAX_MSG_NR];
} ekbox_head;

typedef struct ekbox_record_struct {
	unsigned long msgno;
	size_t index;
	size_t maxsize;
	char   *buf;

	/* the messages of one region have show all */
	int has_show_head;
} ekbox_record;

/* ekbox lock to deal with concurrency */
static DEFINE_SPINLOCK(ekbox_sync_lock);

/* lock flag for reading ekbox */
static unsigned long flags_r;

/* head info of ekbox region */
static ekbox_head *ekbox_head_info;

/* start logical addr of ekbox region */
static char *ekbox_start_vir;

/* start logical addr of ekbox meassage */
char *ekbox_pcontent;

/* ekbox region physical size */
static unsigned long ekbox_size;

/* ekbox region physical addr */
static phys_addr_t ekbox_addr;
#ifdef CONFIG_RTOS_HAL_CORE_RESET_LOG
volatile phys_addr_t core_reset_log_addr;
#endif

/* set ekbox region max msgnr */
unsigned long ekbox_max_msgnr = MAX_MSG_NR;

/*
 * param: ekbox_on_working
 * when param 'ekbox' not config, defualt set it 0.
 * when regions register failure, set it 0.
 * when regions register success, set it 1.
 * when set it '0', ekbox not work and not create file /proc/ekbox.
 * when set it '1', enable ekbox to record message and create file /proc/ekbox.
 */
static int ekbox_on_working;

static void __init ekbox_param_msgnr_parse(const char *cmdline_ekbox)
{
	const char *str_start = NULL;
	char *str_end = NULL;

	str_start = cmdline_ekbox;
	if (*str_start == ',') {
		if (strlen(str_start) <= 1)
			goto msgnr_err;

		str_start = str_start + 1;
		ekbox_max_msgnr = simple_strtol(str_start, &str_end, 0);
		if ((str_end == str_start) || (ekbox_max_msgnr < 1))
			goto msgnr_err;

		if (ekbox_max_msgnr > MAX_MSG_NR) {
			ekbox_max_msgnr = MAX_MSG_NR;
			ekbox_printk("adjust ekbox max_msgnr to '5' !\n");
		}
	} else {
		ekbox_max_msgnr = MAX_MSG_NR;
	}
	return;

msgnr_err:
	ekbox_on_working = 0;
	ekbox_printk("config 'ekbox' param error!\n");
}

/*
 * Function : ekbox_param_parse
 * Description  : parse cmdline_ekbox to get ekbox param 'ekbox_addr' and 'ekbox_size',
 * ekbox param 'ekbox_max_msgnr' user can config or not.
 */
int __init ekbox_param_parse(char *str)
{
	char *str_start = str;
	char *str_end = NULL;
	unsigned long min_size;

	ekbox_size = simple_strtol(str_start, &str_end, 0);
	if (*str_end != '$')
		goto err;
	if (strlen(str_end) <= 1)
		goto err;

	str_start = str_end + 1;
	ekbox_addr = (phys_addr_t)simple_strtoll(str_start, &str_end, 0);
	if (str_end == str_start)
		goto err;

	/* adjust ekbox_addr 'sizeof(unsigned long)' bytes align */
	ekbox_addr = ALIGN(ekbox_addr, sizeof(phys_addr_t));

	/* parse ekbox param 'max_msgnr', user can set it */
	ekbox_param_msgnr_parse(str_end);
	min_size = HEAD_INFO_OFFSET + (NUM_MAGIC_FLAGS * EKBOX_MAGIC_LEN + EKBOX_RESERVE_SIZE) * ekbox_max_msgnr + EKBOX_MAGIC_LEN;
	if ((ekbox_size < min_size) || (ekbox_max_msgnr < 1))
		goto err;

	ekbox_size = (EKBOX_PER_MSG_LEN - EKBOX_PER_MSG_LEN % 4) * ekbox_max_msgnr + HEAD_INFO_OFFSET + EKBOX_MAGIC_LEN;
#ifdef CONFIG_ARM64
	memblock_add(ekbox_addr, ekbox_size);
	if ((memblock_reserve(ekbox_addr, ekbox_size) != 0) || (memblock_mark_nomap(ekbox_addr, ekbox_size) != 0)) {
		memblock_remove(ekbox_addr, ekbox_size);
		goto err;
	}
#endif
	ekbox_on_working = 1;
	return 0;

err:
	ekbox_on_working = 0;
	ekbox_printk("config 'ekbox' param error!\n");
	return -EINVAL;
}

early_param("ekbox", ekbox_param_parse);
/*
 * parse the region message if validate by EKBOX_MESG_MAGIC and mesg_len.
 * validate reurn 1, read the region message and output.
 * return -ENODEV, if the region EKBOX_MESG_MAGIC  not validate.
 * return 0, if mesg_len is 0.
 */
static int ekbox_mesg_validate(unsigned long msgno)
{
	char *psmagic = NULL;
	unsigned long start_magic;
	unsigned long end_magic;

	psmagic = (char *)(ekbox_start_vir + HEAD_INFO_OFFSET + EKBOX_PER_MSG_LEN * msgno);
	start_magic = *(unsigned long *)psmagic;
	psmagic = (char *)(ekbox_start_vir + HEAD_INFO_OFFSET +
			   EKBOX_PER_MSG_LEN * (msgno + 1) - EKBOX_MAGIC_LEN);
	end_magic = *(unsigned long *)psmagic;

	if ((start_magic != EKBOX_MESG_MAGIC) || (end_magic != EKBOX_MESG_MAGIC)) {
		printk(KERN_ERR "ekbox region:%lu magic validate fail:start_magic=0x%lx, end_magic=0x%lx\n",
		       msgno, start_magic, end_magic);
		return -ENODEV;
	}

	if ((ekbox_head_info->mesg_len[msgno] <= EKBOX_PER_MSG_LEN - NUM_MAGIC_FLAGS * EKBOX_MAGIC_LEN) &&
	    (ekbox_head_info->mesg_len[msgno]) > 0)
		return 1;

	return 0;
}

/*
 * Function     : ct_seq_start
 * Description  : called when read info from ekbox,
 * when ct_seq_next return NULL, when the info too long.
 */
static void *ct_seq_start(struct seq_file *s, loff_t *pos)
{
	spin_lock_irqsave(&ekbox_sync_lock, flags_r);
	if (check_syslog_permissions(SYSLOG_ACTION_READ_ALL, SYSLOG_FROM_PROC)) {
		ekbox_printk("check_syslog_permissions fail on read\n");
		return NULL;
	}
	return (*pos < ekbox_max_msgnr) ? pos : NULL;
}

static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
	(*pos)++;
	return (*pos < ekbox_max_msgnr) ? pos : NULL;
}

/* called when has show message all, or ct_seq_start return NULL */
static void ct_seq_stop(struct seq_file *s, void *v)
{
	spin_unlock_irqrestore(&ekbox_sync_lock, flags_r);
}

/* show the ekbox info when cat '/proc/ekbox' */
static int ct_seq_show(struct seq_file *s, void *v)
{
	unsigned long msgno = *(loff_t *)v;
	char *buf = NULL;
	size_t maxsize;
	size_t len = 0;
	size_t once_len;
	int ret;

	/* parse the region message if validate */
	ret = ekbox_mesg_validate(msgno);
	if (ret <= 0)
		return ret;

	/* one region head info: index, buf_addr, length */
	buf = ekbox_start_vir + HEAD_INFO_OFFSET +
	      msgno * EKBOX_PER_MSG_LEN + EKBOX_MAGIC_LEN;
	maxsize = ekbox_head_info->mesg_len[msgno];

	/*
	 * start to read a new region message,
	 * output the region head info, not always called
	 */
	seq_printf(s, "\nEKbox head info:phyaddr=%pa, physize=0x%lx, msgno=%u\n",
			&ekbox_addr,
			ekbox_head_info->physize,
			ekbox_head_info->msgno);
	seq_printf(s, "EKbox show message[%lx, 0x%lx]:\n", msgno,
			ekbox_head_info->mesg_len[msgno]);

	/* show ekbox message, always called */
	while (len < maxsize) {
		once_len = maxsize - len;
		if (once_len > EKBOX_ONCE_SHOW_LEN)
			once_len = EKBOX_ONCE_SHOW_LEN;

		seq_write(s, buf + len, once_len);
		len += once_len;
	}

	return 0;
}

static const struct seq_operations ct_seq_ops = {
	.start = ct_seq_start,
	.next  = ct_seq_next,
	.stop  = ct_seq_stop,
	.show  = ct_seq_show
};

static int ct_open(struct inode *inode, struct file *file)
{
	if (check_msg_restrict()) {
		ekbox_printk("check permissions fail on open\n");
		return -EPERM;
	}
	return seq_open(file, &ct_seq_ops);
}

/* write ekbox to format or stop ekbox */
static ssize_t ekbox_proc_write(struct file *file, const char __user *buffer,
				size_t size, loff_t *off)
{
	char loc_buf[2];
	unsigned long flags;

	if (check_syslog_permissions(SYSLOG_ACTION_CLEAR, SYSLOG_FROM_PROC)) {
		ekbox_printk("check_syslog_permissions fail on write\n");
		return -EPERM;
	}

	if (size != 2) {
		ekbox_printk("input string error!\n");
		return -EINVAL;
	}

	if (copy_from_user(loc_buf, buffer, size)) {
		ekbox_printk("failed to copy from user!\n");
		return -EINVAL;
	}

	switch (loc_buf[0]) {
	case 'c':
	case 'C':
			/*
			 * ekbox format, stop ekbox before format ekbox,
			 * to avoid continue write message to ekbox.
			 * and the reboot info also not be recorded.
			 */
			ekbox_on_working = 0;
			ekbox_stop_record_printk();
			spin_lock_irqsave(&ekbox_sync_lock, flags);
			ekbox_format_memory();
			spin_unlock_irqrestore(&ekbox_sync_lock, flags);
			ekbox_printk("ekbox format success!\n");
			ekbox_on_working = 1;
			break;
	case 's':
	case 'S':
			/* ekbox stop and not record printk message */
			ekbox_stop_record_printk();
			ekbox_printk("ekbox stop success!\n");
			break;
	default:
			ekbox_printk("input string error!\n");
			return -EINVAL;
	}

	return size;
}

static const struct proc_ops ct_proc_ops = {
	.proc_open    = ct_open,
	.proc_read    = seq_read,
	.proc_lseek   = seq_lseek,
	.proc_release = seq_release,
	.proc_write   = ekbox_proc_write
};

/* format the memory of ekbox */
static void ekbox_format_memory(void)
{
	unsigned long msgno;
	unsigned long *pmagic = NULL;

	memset(ekbox_start_vir, 0, ekbox_size);

	/* init ekbox region magic flag */
	*EKBOX_START_MAGIC = EKBOX_RESERVED_MEMORY_MAGIC;
	*EKBOX_END_MAGIC = EKBOX_RESERVED_MEMORY_MAGIC;

	/* init ekbox region phyaddr and physize */
	ekbox_head_info->phyaddr = (unsigned long)ekbox_addr;
	ekbox_head_info->physize = ekbox_size;

	/* init the message region */
	ekbox_head_info->msgno = 0;
	ekbox_head_info->max_msgnr = ekbox_max_msgnr;
	ekbox_head_info->version[0] = 1;
	ekbox_head_info->version[1] = 0;

	ekbox_head_info->checksum = ekbox_head_info->phyaddr +
				    ekbox_head_info->physize + ekbox_head_info->msgno;
	for (msgno = 0; msgno < ekbox_max_msgnr; msgno++) {
		/* head magic flag of message region */
		pmagic = (unsigned long *)(ekbox_start_vir + HEAD_INFO_OFFSET + EKBOX_PER_MSG_LEN * msgno);
		*pmagic = EKBOX_MESG_MAGIC;

		/* tail magic flag of message region */
		pmagic = (unsigned long *)(ekbox_start_vir + HEAD_INFO_OFFSET +
					   EKBOX_PER_MSG_LEN * (msgno + 1) - EKBOX_MAGIC_LEN);
		*pmagic = EKBOX_MESG_MAGIC;
	}

	/* init logical addr of ekbox message */
	ekbox_pcontent = ekbox_start_vir + HEAD_INFO_OFFSET + EKBOX_MAGIC_LEN;
	ekbox_head_info->mesg_len[ekbox_head_info->msgno] = EKBOX_RESERVE_SIZE;

	atomic_notifier_call_chain(&ekbox_reserved_memory_list, 0, NULL);
}

static bool ekbox_need_format(void)
{
	int ret;
	unsigned long msgno;

	if (ekbox_head_info->phyaddr != (unsigned long)ekbox_addr) {
		ekbox_printk("physical addr changed, need format the memory!\n");
		return true;
	}

	if (ekbox_head_info->physize != ekbox_size) {
		ekbox_printk("memory size changed, need format the memory!\n");
		return true;
	}

	if (ekbox_max_msgnr != ekbox_head_info->max_msgnr) {
		ekbox_printk("max msgnr changed, need format the memory!\n");
		return true;
	}

	if (ekbox_head_info->checksum != (ekbox_head_info->phyaddr +
					  ekbox_head_info->physize + ekbox_head_info->msgno)) {
		ekbox_printk("checksum changed, need format the memory!\n");
		return true;
	}

	for (msgno = 0; msgno < ekbox_max_msgnr; msgno++) {
		ret = ekbox_mesg_validate(msgno);
		if (ret < 0)
			return true;
	}

	return false;
}

/*
 * Function     : ekbox_buffer_register
 * Description : register ekbox buffer
 * Return       : success return 0, fail return -EINVAL
 */
static __init int ekbox_buffer_register(void)
{
	/*
	 * ekbox regions:
	 *
	 * |--8--|--head info--|-------------------------------------------|--8--|
	 *                     |---------------------&---------------------|
	 *                     |--8--|--content-|--8-&-8--|--content-|--8--|
	 *
	 * '2' means one message region has two magic flags
	 */
	unsigned long start_magic;
	unsigned long end_magic;

	start_magic = *(unsigned long *)EKBOX_START_MAGIC;
	end_magic = *(unsigned long *)EKBOX_END_MAGIC;

	if (ekbox_size <= (HEAD_INFO_OFFSET + (2 * ekbox_max_msgnr + 1) * EKBOX_MAGIC_LEN))
		return -EINVAL;

	/*
	 * the region need format always,
	 * no need format only at ekbox_size, ekbox_addr, ekbox_max_msgnr and checksum not change.
	 */
	if ((EKBOX_RESERVED_MEMORY_MAGIC == start_magic) &&
	    (EKBOX_RESERVED_MEMORY_MAGIC == end_magic)) {
		if (ekbox_need_format())
			goto format;
		/*
		 * when "echo c > /proc/ekbox",then reboot os, the value of
		 * ekbox_head_info->mesg_len[ekbox_head_info->msgno] maybe 0
		 */
		if (ekbox_head_info->mesg_len[ekbox_head_info->msgno] != 0)
			ekbox_head_info->msgno = ((ekbox_head_info->msgno + 1) % ekbox_max_msgnr);
		ekbox_head_info->mesg_len[ekbox_head_info->msgno] = EKBOX_RESERVE_SIZE;
		ekbox_head_info->checksum = ekbox_head_info->phyaddr +
					    ekbox_head_info->physize + ekbox_head_info->msgno;
		ekbox_pcontent = ekbox_start_vir + HEAD_INFO_OFFSET +
				 (ekbox_head_info->msgno) * EKBOX_PER_MSG_LEN + EKBOX_MAGIC_LEN;
		memset(ekbox_pcontent, 0, EKBOX_PER_MSG_LEN - EKBOX_MAGIC_LEN * NUM_MAGIC_FLAGS);
		return 0;
	}
format:
	ekbox_format_memory();
	return 0;
}


/*
 * Function    : ekbox_console_write
 * Description : write the content to the message region
 */
static void ekbox_console_write(struct console *con, const char *buf, unsigned int len)
{
	unsigned long flags;
	unsigned long residuary_length, cpylen;

	/*
	 * when the ekbox reserved memory area is destroyed, the value of CURRENT_MSG_LEN
	 * may be modified and beyond the length of EKBOX_PER_MSG_LEN.
	 * NUM_MAGIC_FLAGS means one message region has two magic flags
	 */
	if (CURRENT_MSG_LEN < (EKBOX_PER_MSG_LEN - EKBOX_MAGIC_LEN * NUM_MAGIC_FLAGS)) {
		spin_lock_irqsave(&ekbox_sync_lock, flags);
		residuary_length = EKBOX_PER_MSG_LEN - CURRENT_MSG_LEN - EKBOX_MAGIC_LEN * NUM_MAGIC_FLAGS;
		cpylen = residuary_length > len ? len : residuary_length;
		memcpy(ekbox_pcontent + CURRENT_MSG_LEN, buf, cpylen);
		ekbox_head_info->mesg_len[ekbox_head_info->msgno] += (cpylen);
		spin_unlock_irqrestore(&ekbox_sync_lock, flags);
	}
}


/* console structure for capturing the log outputed by printk */
static struct console ekbox_console = {
	.flags =  CON_ENABLED | CON_PRINTBUFFER,
	.write = ekbox_console_write,
};

void ekbox_record_point_fmt(const char *fmt, ...)
{
	va_list args;
	int len = 0;
	unsigned int ret = 0;
	char buf[EKBOX_BUFFER_SIZE];

	if ((!ekbox_on_working) || (fmt == NULL))
		return;

	va_start(args, fmt);
	ret = strlcpy(buf, EKBOX_AFFIX, EKBOX_BUFFER_SIZE);
	len = vscnprintf(buf + strlen(buf), EKBOX_BUFFER_SIZE - strlen(buf),
			 fmt, args);
	va_end(args);

	ekbox_console_write(NULL, buf, (len + ret));
}

void ekbox_record_point(const char *buf, size_t len)
{
	if ((!ekbox_on_working) || (buf == NULL) || (len <= 0))
		return;

	ekbox_console_write(NULL, buf, len);
}
EXPORT_SYMBOL(ekbox_record_point);

/* mainly register ekbox buffer */
int __init ekbox_init(void)
{
	if (!ekbox_on_working)
		return -EINVAL;
#ifdef CONFIG_ARM64
#ifdef CONFIG_RTOS_KASAN
	void *start = (void *)__phys_to_virt(ekbox_addr);
	void *end = (void *)__phys_to_virt(ekbox_addr + (phys_addr_t)ekbox_size);

	/*
	 * end + 1 here is intentional. We check several shadow bytes in
	 * advance to slightly speed up fastpath. In some rare cases
	 * we could cross boundary of mapped shadow, so we just map
	 * some more here.
	 */
	kasan_map_populate_ext((unsigned long)kasan_mem_to_shadow(start),
			 (unsigned long)kasan_mem_to_shadow(end),
			 early_pfn_to_nid(virt_to_pfn(start)));

	memset(kasan_mem_to_shadow(start), 0,
		(unsigned long)kasan_mem_to_shadow(end) - (unsigned long)kasan_mem_to_shadow(start));
#endif

	ekbox_start_vir = (char *)__phys_to_virt(ekbox_addr);
	ekbox_create_mapping(ekbox_addr, __phys_to_virt(ekbox_addr), (phys_addr_t)ekbox_size, __pgprot(PROT_NORMAL_NC));
#endif
#if defined(CONFIG_ARM) || defined(CONFIG_X86) || defined(CONFIG_PPC)
	ekbox_start_vir = ekbox_mm_create((resource_size_t)ekbox_addr, ekbox_size);
#endif
	if (ekbox_start_vir == NULL) {
		ekbox_printk("ekbox_init early_ioremap memory error!\n");
		goto init_failed;
	}

	ekbox_head_info = (ekbox_head *)(ekbox_start_vir + EKBOX_MAGIC_LEN);
	if (0 > ekbox_buffer_register()) {
#if defined(CONFIG_ARM) || defined(CONFIG_X86) || defined(CONFIG_PPC)
		ekbox_mm_clean((char *)ekbox_start_vir, ekbox_size);
#endif
		ekbox_printk("ekbox buffer register fail!\n");
		goto init_failed;
	}

	spin_lock_init(&ekbox_sync_lock);

#if defined(CONFIG_RTOS_KASAN) && defined(CONFIG_PPC)
	kasan_init_region(ekbox_start_vir, ekbox_size);
#endif

	return 0;

init_failed:
	ekbox_on_working = 0;
	return -EINVAL;
}


/*
 * Function    : ekbox_reinit
 * Description : ioremap the ekbox addr, register nolevel console.
 * Return      : failure return -1, success return 0.
 * Caller      : called in start_kernel(),after mm_init.
 */
int __init ekbox_reinit(void)
{
	if (!ekbox_on_working)
		return -EINVAL;
#if defined(CONFIG_ARM) || defined(CONFIG_X86)
	ekbox_mm_clean((char *)ekbox_start_vir, ekbox_size);
#endif

#ifndef CONFIG_PPC
	/*
	 * Powerpc don't support discontiguous System Ram.
	 * When reserved memory of ekbox is in System Ram, it will cause
	 * following situations:
	 * 1. If the no-map attribute is added into reserved-memory node
	 *    in dts file, only first contiguous memory region of System
	 *    Ram will be used;
	 * 2. If the no-map attribute is not added into reserved-memory node
	 *    in dts file or if use /memreserve/ to reserve memory from System
	 *    Ram, the System Ram fails to register into the resource.
	 * To ensure ekbox reliability in powerpc, disable request_mem_region
	 * before ioremap for ekbox in powerpc.
	 */
#ifndef CONFIG_ARM64
	if (!request_mem_region(ekbox_addr, (resource_size_t)ekbox_size, "ekbox_ram")) {
		ekbox_on_working = 0;
		pr_err("ekbox request mem region failed\n");
		return -EINVAL;
	}
#endif
	ekbox_start_vir = (char *)ioremap_wc(ekbox_addr, ekbox_size);
	if (ekbox_start_vir == NULL) {
		ekbox_on_working = 0;
		ekbox_printk("ekbox_reinit ioremap memory error!\n");
#ifndef CONFIG_ARM64
		release_mem_region(ekbox_addr, ekbox_size);
#endif
		return -EINVAL;
	}
#endif

	ekbox_head_info = (ekbox_head *)(ekbox_start_vir + EKBOX_MAGIC_LEN);

#ifdef CONFIG_RTOS_HAL_CORE_RESET_LOG
	core_reset_log_addr = ekbox_addr + HEAD_INFO_OFFSET +
			 (ekbox_head_info->msgno) * EKBOX_PER_MSG_LEN +
			 EKBOX_MAGIC_LEN;
#endif

	ekbox_pcontent = ekbox_start_vir + HEAD_INFO_OFFSET +
			 (ekbox_head_info->msgno) * EKBOX_PER_MSG_LEN +
			 EKBOX_MAGIC_LEN;

	register_nolevel_console(&ekbox_console);

	return 0;
}


/*
 * Function : ekbox_stop_record_printk
 * Description : stop the ekbox, stop record the kernek 'printk' message.
 * if need record can use function ekbox_record_point.
 */
static void ekbox_stop_record_printk(void)
{
	if (nolevel_console_drivers == &ekbox_console)
		register_nolevel_console(NULL);
}


static int __init ekbox_create_proc(void)
{
	struct proc_dir_entry *ekbox_entry = NULL;

	if (!ekbox_on_working)
		return -1;

	/* creat proc file about ekbox */
	ekbox_entry = proc_create("ekbox", S_IRUSR | S_IRGRP | S_IWUSR | S_IROTH, NULL, &ct_proc_ops);
	if (!ekbox_entry) {
		ekbox_printk("ekbox proc create fail\n");
		return -ENOMEM;
	}
	return 0;
}

module_init(ekbox_create_proc);
MODULE_LICENSE("GPL");

